using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace XFGameFramework.TweenSystem
{

    /// <summary>
    /// Text动画拓展
    /// </summary>
    public static class TextExtensions  
    {
        /// <summary>
        /// 渐变文本
        /// </summary>
        /// <param name="text">Text对象</param>
        /// <param name="target">目标值</param>
        /// <param name="duration">持续时间</param>
        /// <param name="loop">循环模式</param>
        /// <param name="ease">缓动曲线</param>
        /// <param name="curve">自定义的缓动曲线</param>
        /// <param name="ingoreTimeScale">是否忽略时间缩放</param>
        /// <param name="delay">延迟</param>
        /// <param name="loopCount">循环次数</param>
        /// <returns></returns>
        public static TweenerCore<float> TweenText(this Text text, string target, float duration, LoopMode loop = LoopMode.Once, Ease ease = Ease.Linear, AnimationCurve curve = null, bool ingoreTimeScale = false, float delay = 0,int loopCount = -1)
        {
            text.text = string.Empty;
            Action<TweenerCore<float>> callback = (TweenerCore<float> t) =>
            {
                text.text = target.Substring(0,(int)Mathf.Lerp(0,target.Length,t.Value));
            };

            return TweenManager.ComponentTween(text, 0, 1, duration, loop, ease, curve, ingoreTimeScale, callback, delay,0,loopCount);
        }

        /// <summary>
        /// 渐变文本
        /// </summary>
        /// <param name="text">TextMesh对象</param>
        /// <param name="target">目标值</param>
        /// <param name="duration">持续时间</param>
        /// <param name="loop">循环模式</param>
        /// <param name="ease">缓动曲线</param>
        /// <param name="curve">自定义的缓动曲线</param>
        /// <param name="ingoreTimeScale">是否忽略时间缩放</param>
        /// <param name="delay">延迟</param>
        /// <param name="loopCount">循环次数</param>
        /// <returns></returns>
        public static TweenerCore<float> TweenText(this TextMesh text, string target, float duration, LoopMode loop = LoopMode.Once, Ease ease = Ease.Linear, AnimationCurve curve = null, bool ingoreTimeScale = false, float delay = 0, int loopCount = -1)
        {
            text.text = string.Empty;
            Action<TweenerCore<float>> callback = (TweenerCore<float> t) =>
            {
                text.text = target.Substring(0, (int)Mathf.Lerp(0, target.Length, t.Value));
            };

            return TweenManager.ComponentTween(text, 0, 1, duration, loop, ease, curve, ingoreTimeScale, callback, delay,0,loopCount);
        }

        /// <summary>
        /// 渐变颜色
        /// </summary>
        /// <param name="graphic">TextMesh对象</param>
        /// <param name="target">目标颜色</param>
        /// <param name="duration">持续时间</param>
        /// <param name="loop">循环模式</param>
        /// <param name="ease">缓动曲线</param>
        /// <param name="curve">自定义的缓动曲线</param>
        /// <param name="ingoreTimeScale">是否忽略时间缩放</param>
        /// <param name="delay">延迟</param>
        /// <param name="loopCount">循环次数</param>
        /// <returns></returns>
        public static TweenerCore<Color> TweenColor(this TextMesh graphic, Color target, float duration, LoopMode loop = LoopMode.Once, Ease ease = Ease.Linear, AnimationCurve curve = null, bool ingoreTimeScale = false, float delay = 0, int loopCount = -1)
        {
            return TweenManager.ComponentTween(graphic, graphic.color, target, duration, loop, ease, curve, ingoreTimeScale, OnTweenColor, delay,0,loopCount);
        }

        /// <summary>
        /// 渐变透明度
        /// </summary>
        /// <param name="graphic">TextMesh对象</param>
        /// <param name="target">目标透明度</param>
        /// <param name="duration">持续时间</param>
        /// <param name="loop">循环模式</param>
        /// <param name="ease">缓动曲线</param>
        /// <param name="curve">自定义的缓动曲线</param>
        /// <param name="ingoreTimeScale">是否忽略时间缩放</param>
        /// <param name="delay">延迟</param>
        /// <param name="loopCount">循环次数</param>
        /// <returns></returns>
        public static TweenerCore<float> TweenFade(this TextMesh graphic, float target, float duration, LoopMode loop = LoopMode.Once, Ease ease = Ease.Linear, AnimationCurve curve = null, bool ingoreTimeScale = false, float delay = 0, int loopCount = -1)
        {
            return TweenManager.ComponentTween(graphic, graphic.color.a, target, duration, loop, ease, curve, ingoreTimeScale, OnTweenFade, delay, 0, loopCount);
        }


        internal static void OnTweenColor(TweenerCore<Color> tweener)
        {
            if (tweener.Component is TextMesh)
            {
                TextMesh graphic = tweener.Component as TextMesh;
                graphic.color = tweener.Value;
            }
        }

        internal static void OnTweenFade(TweenerCore<float> tweener)
        {
            if (tweener.Component is TextMesh)
            {
                TextMesh graphic = tweener.Component as TextMesh;
                graphic.color = new Color(graphic.color.r, graphic.color.g, graphic.color.b, tweener.Value);
            }
        }

    }

}

